package com.smart.dao;


import com.smart.domain.ComputerRoom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;


@Repository // 通过spring注解定义一个Dao
public class ComputerRoomDao {


    private JdbcTemplate jdbcTemplate;

    @Autowired // 自动注入jdbcTemplate 的bean
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    //插入
    public int  addCompRoom(  ComputerRoom computerRoom) {

      final  String sql = "insert into user_computerroom (Room_id,Room_name,Room_zone,Room_buildingnumber,Room_floor,Room_roomnumber,Room_seat) VALUES(?,?,?,?,?,?,?)";
       Object[] params= new Object[]{computerRoom.getRoom_id(), computerRoom.getRoom_name(), computerRoom.getRoom_zone(), computerRoom.getRoom_buildingnumber(),computerRoom.getRoom_floor(),computerRoom.getRoom_roomnumber(),computerRoom.getRoom_seat()};
       return jdbcTemplate.update(sql,params);
    }
    /**
     * 修改，但我发现主码跟唯一值不能修改
     * @param
     * @param
     * @ return
     */
    public int updateCompRoom( ComputerRoom computerRoom1){
        final String sqlt = "update user_computerroom set Room_floor= ? where Room_buildingnumber= ?";
        return jdbcTemplate.update(sqlt,computerRoom1.getRoom_floor(),computerRoom1.getRoom_buildingnumber());

    }
    /**
     * 删除
     * @param
     * @param
     * @ return
     */
    public int deleteCompRoom(ComputerRoom computerRoom){
        final String sql ="delete from user_computerroom where Room_name =?";
        return jdbcTemplate.update(sql,computerRoom.getRoom_name());
    }


    /**
     * 查询单个对象
     * @param
     * @param
     * @ return
     */
    public void queryCompRoom(ComputerRoom computerRoom)
    {
        String sql ="select * from user_computerroom where Room_id =?";
        ComputerRoom com =jdbcTemplate.queryForObject(sql,new MyRowMapper(),computerRoom.getRoom_id());
        System.out.println(com);
    }



    /**
     * 查询集合对象
     * @param
     * @param
     * @ return
     */
    public void querylistCompRoom(ComputerRoom computerRoom){
        String sql ="select * from user_computerroom";
        List<ComputerRoom> com =jdbcTemplate.query(sql,new MyRowMapper());
        System.out.println(com);
    }
}
/**
 * 封装MyRowMaper类
 * @param
 * @param
 * @ return
 */
class  MyRowMapper implements RowMapper<ComputerRoom>
        {
            @Override
            public ComputerRoom mapRow(ResultSet rs, int i) throws SQLException {
                //1 从结果集里得到数据
                 int id =rs.getInt("Room_id");
                 String name=rs.getString("Room_name");
                 String zone=rs.getString("Room_zone");
                 int buildingnumber=rs.getInt("Room_buildingnumber");
                 int floor=rs.getInt("Room_floor");
                 int roomnumber=rs.getInt("Room_roomnumber");
                 int seat=rs.getInt("Room_seat");
                 //2 把得到数据封装到数据里面去
                ComputerRoom com =new ComputerRoom();
                com.setRoom_id(id );
                com.setRoom_name(name);
                com.setRoom_zone(zone);
                com.setRoom_buildingnumber(buildingnumber);
                com.setRoom_floor(floor);
                com.setRoom_roomnumber(roomnumber);
                com.setRoom_seat(seat);
                return com;
            }
        }

